<?php

namespace App\Exports;

use App\Models\finance\ReceivableTemplate1Model;
use App\Models\TakeOrderModel;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class ReceivableTemplate1Export implements FromCollection, WithHeadings, WithMapping
{
    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function collection()
    {
        $query = ReceivableTemplate1Model::with(['take_order', 'create_admin']);
        
        // 筛选条件
        if ($this->request->filled('take_order_id')) {
            $query->where('take_order_id', $this->request->input('take_order_id'));
        }
        
        if ($this->request->filled('take_order_sn')) {
            $takeOrderIds = TakeOrderModel::where('sn', 'like', '%' . $this->request->input('take_order_sn') . '%')
                ->pluck('id')
                ->toArray();
            if (!empty($takeOrderIds)) {
                $query->whereIn('take_order_id', $takeOrderIds);
            }
        }
        
        if ($this->request->filled('origin')) {
            $query->where('origin', 'like', '%' . $this->request->input('origin') . '%');
        }
        
        if ($this->request->filled('destination')) {
            $query->where('destination', 'like', '%' . $this->request->input('destination') . '%');
        }
        
        if ($this->request->filled('status')) {
            $query->where('status', $this->request->input('status'));
        }
        
        if ($this->request->filled('currency')) {
            $query->where('currency', $this->request->input('currency'));
        }
        
        if ($this->request->filled('business_date_start') && $this->request->filled('business_date_end')) {
            $query->whereBetween('business_date', [$this->request->input('business_date_start'), $this->request->input('business_date_end')]);
        }
        
        if ($this->request->filled('date_start') && $this->request->filled('date_end')) {
            $query->whereBetween('created_at', [$this->request->input('date_start'), $this->request->input('date_end') . ' 23:59:59']);
        }

        return $query->orderBy('id', 'desc')->get();
    }

    public function headings(): array
    {
        return [
            'ID',
            '业务日期',
            '主单号',
            '起始港',
            '目的港',
            '计费重',
            '毛重',
            '体积重',
            '大包数量',
            '体积',
            '空运单价',
            '币种',
            '空运费',
            '主单费',
            '验电费',
            '操作费-计费重',
            '包车费',
            '香港登记费',
            '境外卡转费',
            '保险费',
            '海运运费',
            '地面操作费-空运',
            '总额',
            '状态',
            '创建人',
            '创建时间',
        ];
    }

    public function map($item): array
    {
        $statusMap = [
            1 => '未付款',
            2 => '部分付款',
            3 => '已付款',
        ];

        return [
            $item->id,
            $item->business_date,
            $item->take_order?->sn ?? '',
            $item->origin,
            $item->destination,
            $item->charging_weight,
            $item->gross_weight,
            $item->volume_weight,
            $item->big_package,
            $item->volume,
            $item->air_freight_unit_price,
            $item->currency,
            $item->air_freight,
            $item->master_bill_fee,
            $item->electricity_check_fee,
            $item->operation_fee_charging_weight,
            $item->charter_fee,
            $item->hk_registration_fee,
            $item->cross_border_transfer_fee,
            $item->insurance_fee,
            $item->sea_freight,
            $item->ground_operation_fee_air,
            $item->total_amount,
            $statusMap[$item->status] ?? '未知',
            $item->create_admin?->name ?? '',
            $item->created_at->format('Y-m-d H:i:s'),
        ];
    }
} 